<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="上级部门" prop="parentId">
            <treeselect
              v-model="form.parentId"
              :options="deptList"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级部门"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="部门名称" prop="deptName">
            <el-input v-model="form.deptName" placeholder="请输入部门名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示排序" prop="orderNum">
            <el-input v-model="form.orderNum" placeholder="请输入显示排序码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio v-for="status in statusOptions" :key="status.dictValue" :label="status.dictValue">
                {{ status.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="负责人" prop="leader">
            <el-input v-model="form.leaderName" style="width: 80%" disabled placeholder="请输入负责人"/>
            <el-button type="primary" style="width: 15%;margin-left: 5px" @click="selectLeader">选择</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="form.phone" disabled placeholder="请输入联系电话" controls-position="right" :min="0"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" disabled placeholder="请输入邮箱" controls-position="right" :min="0"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :sapn="24" style="text-align: center">
          <el-button type="primary" @click="add">添加</el-button>
        </el-col>
      </el-row>
    </el-form>

    <!--选择用户的组件-->
    <select-system-user ref="selectUser" v-if="showSelectUserDialog" @select="confirmSelectUser"></select-system-user>
  </div>
</template>

<script>
import sysDeptApi from "@/api/system/sysDept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import selectSystemUser from '@/views/components/selectSystemUser/index'

export default {
  name: "sys-dept-add",
  components: {
    Treeselect,selectSystemUser
  },
  props: {
    activeObj: {
      type: Object,
      default: undefined
    }
  },
  watch: {
    activeObj: {
      immediate: true,
      handler: function (newVal, oldVal) {
        if (newVal) {
          this.form.parentId = newVal.deptId
        } else {
          this.form.parentId = 0
        }
      }
    }
  },
  data() {
    return {
      //保存的数据
      form: {
        parentId: undefined,
        deptName: undefined,
        orderNum: 0,
        leaderId: undefined,
        leaderName: undefined,
        phone: undefined,
        email: undefined,
        status: 0
      },
      // 表单校验
      rules: {
        parentId: [
          {required: true, message: "上级部门不能为空", trigger: "blur"}
        ],
        deptName: [
          {required: true, message: "部门名称不能为空", trigger: "blur"}
        ],
        orderNum: [
          {required: true, message: "显示排序不能为空", trigger: "blur"}
        ]
      },
      //状态字典数据
      statusOptions: [],
      //部门树的数据
      deptList: [],
      //是否打开选择用户的弹出层组件
      showSelectUserDialog: false
    }
  },
  created() {
    this.getDataByType("sys_status_type").then(res => {
      this.statusOptions = res.data;
    })
    this.loadAllDept();
  },
  methods: {
    //加载所有部门数据
    loadAllDept() {
      sysDeptApi.listDept().then(res => {
        this.deptList = [];
        const dept = {deptId: 0, deptName: '顶级部门', children: res.data};
        this.deptList = [dept];
      }).catch(() => {
      })
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    //保存部门
    add() {
      this.$refs.form.validate(valid => {
        if (valid) {
          sysDeptApi.addDept(this.form).then(res => {
            this.$message.success(res.msg);
            //调用父组件里面绑定的after事件
            this.$emit("after")
            //调用父组件里面绑定的close事件
            this.$emit("close")
          })
        }
      })
    },
    //选择直属领导
    selectLeader() {
      this.showSelectUserDialog = true;
      this.$nextTick(_ => {
        this.$refs.selectUser.initSelectUser();
      })
    },
    //选择用户的弹出层触发之后确定之后回调的方法
    confirmSelectUser(user) {
      this.form.leaderId = user.userId;
      this.form.leaderName = user.nickName;
      this.form.phone = user.phone;
      this.form.email = user.email;
    }
  }
}
</script>

<style scoped>

</style>
